<template>
  <header class="header">
    <h1>todos</h1>

    <input v-model="checkAll" id="toggle-all" class="toggle-all" type="checkbox" />
    <label for="toggle-all"></label>
    <input
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
      v-model.trim="task"
      @keyup.enter="add"
    />
  </header>
</template>

<script>
export default {
  props: {
    list: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      task: ''
    }
  },
  methods: {
    add () {
      // 非空判断
      if (this.task === '') return alert('请输入任务名')

      // 能来到这代表不为空，可以添加
      // 添加到数组里，它不能改父的数组，所以做通知
      this.$emit('add', this.task)
      // 清空输入内容
      this.task = ''
    }
  },

  computed: {
    // checkAll () {
    //   return this.list.every(v => v.isDone)
    // }

    checkAll: {
      get () {
        if (this.list.length === 0) return false
        return this.list.every(v => v.isDone)
      },

      set (val) {
        this.list.forEach(v => v.isDone = val)
      }
    }
  }
}
</script>
